package com.health.dao;

import com.health.pojo.ComboForm;
import com.health.pojo.ExamineOrder;
import com.health.pojo.HealthAttributes;
import com.health.utils.ConnectionUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class HealthComboDao {
    public List<ExamineOrder> selectByExamineOrder() {
        Connection connection = ConnectionUtil.getConnection();
        String sql = "select * from examine_order";
        // 定义结果集合
        List<ExamineOrder> list = new ArrayList<>();

        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                int examineId = resultSet.getInt("examine_id");
                String examineCombo = resultSet.getString("examine_combo");
                String examinePrice = resultSet.getString("examine_price");
                ExamineOrder examineOrder = new ExamineOrder();
                examineOrder.setExamineId(examineId);
                examineOrder.setExamineCombo(examineCombo);
                examineOrder.setExaminePrice(examinePrice);
                list.add(examineOrder);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }


    public List<HealthAttributes> selectByExamineId(String examineId) {
        Connection connection = ConnectionUtil.getConnection();
        List<HealthAttributes> healthAttributesList = new ArrayList<>();
        String sql = "select * from order_attribute,health_attribute\n" +
                "where order_attribute.attribute_id = health_attribute.attribute_id and examine_id = ?";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, Integer.parseInt(examineId));
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                int attributeId = resultSet.getInt("attribute_id");
                String attributeName = resultSet.getString("attribute_name");
                String attributeScope = resultSet.getString("attribute_scope");
                HealthAttributes healthAttributes = new HealthAttributes();
                healthAttributes.setAttributeId(attributeId);
                healthAttributes.setAttributeName(attributeName);
                healthAttributes.setAttributeScope(attributeScope);
                healthAttributesList.add(healthAttributes);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return healthAttributesList;
    }

    public boolean insertByExamineOrder(ExamineOrder examineOrder, Integer userId) {
        int resultLine = 0;
        Connection connection = ConnectionUtil.getConnection();
        String sql = "insert into users_examine(user_id, examine_id, examine_date) values (?,?,?)";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, userId);
            preparedStatement.setInt(2, examineOrder.getExamineId());
            preparedStatement.setString(3, examineOrder.getExamineDate());
            resultLine = preparedStatement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return resultLine != 0;
    }

    //order_attribute插入
    public void insertByComboForm(ComboForm comboform) {
        comboform.setExamineId(insertExamineOrder(comboform.getExamineCombo(), comboform.getExaminePrice()));
        Set set = comboform.getHealthlist().keySet();
        if (comboform.getExamineId() != 0) {
            Connection connection = ConnectionUtil.getConnection();
            String sqlstring = "insert into order_attribute values(?,?)";
            try {
                PreparedStatement preparedStatement = connection.prepareStatement(sqlstring);
                for (Object key : set) {
                    System.out.println(key);
                    if (comboform.getHealthlist().get(key).getChecked()) {
                        preparedStatement.setInt(1, comboform.getExamineId());
                        preparedStatement.setObject(2, key);
                        preparedStatement.executeUpdate();
                    }

                }


            } catch (Exception e) {
                e.printStackTrace();

            }

        }


    }

    //examine_order套餐插入
    public int insertExamineOrder(String examine_combo, String examine_price) {
        int examine_id = 0;
        Connection connection = ConnectionUtil.getConnection();
        String sqlstring = "insert into examine_order(examine_combo,examine_price) values(?,?)";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sqlstring, Statement.RETURN_GENERATED_KEYS);
            preparedStatement.setString(1, examine_combo);
            preparedStatement.setString(2, examine_price);
            preparedStatement.executeUpdate();
            ResultSet resultSet = preparedStatement.getGeneratedKeys();
            if (resultSet.next()) {
                examine_id = resultSet.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        return examine_id;

    }

    //根据用户id查询examine_order套餐表
    public void selectComboByUserId(int UID, ArrayList<ComboForm> comboForms) {
        Connection connection = ConnectionUtil.getConnection();

        String sql = "select * from users_examine join examine_order on users_examine.examine_id=examine_order.examine_id where user_id=?";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, UID);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt("examine_id");
                String name = resultSet.getString("examine_combo");
                String time = resultSet.getString("examine_date");
                String price = resultSet.getString("examine_price");


                comboForms.add(new ComboForm(id, name, price, time));

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
